﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_FlatManger_frm_AuditSet : System.Web.UI.Page
{
    public string DataValidator(ArrayList ControlArray)
    {
        string strReturnValue = "";

        IEnumerator ienumer = ControlArray.GetEnumerator();

        while (ienumer.MoveNext())
        {
            try
            {
                CuTextSelectBox txtbox = (CuTextSelectBox)ienumer.Current;

                if (txtbox.Text.Trim() == "")
                {
                    strReturnValue = txtbox.ValidateText + "不能为空！";

                    break;
                }
                else
                {
                    if (txtbox.IsValidateDateTime)
                    {
                        try
                        {
                            DateTime dateTime = DateTime.Parse(txtbox.Text.Trim());
                        }
                        catch
                        {
                            strReturnValue = txtbox.ValidateText + "时间格式不正确！";

                            break;
                        }
                    }
                }
            }
            catch
            {
                CuTextSelectBox txtbox = (CuTextSelectBox)ienumer.Current;

                if (txtbox.Text.Trim() == "")
                {
                    txtbox.ReadOnly = false;
                    strReturnValue = txtbox.ValidateText + "不能为空！";

                    break;
                }
            }
        }

        return strReturnValue;
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindAuditSetByDataView(dst, intPageIndex);
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteAuditSet(intCommandName);
    }

    public void AddAuditSet(object sender, ImageClickEventArgs e)
    {
        if (this.lstBox.SelectedIndex != -1)
        {
            int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

            if (intCommand == 1)
            {
                this.panAddResult.Visible = false;
                this.ctsbAdd0.Text = "";
            }
            else
            {
                //数据验证
                ArrayList controlArray = new ArrayList();
                controlArray.Add(this.ctsbAdd0);

                string strErrorMsg = this.DataValidator(controlArray);

                if (strErrorMsg == "")
                {
                    ClsMenuDAL cls_Menu = new ClsMenuDAL();
                    int intCount = cls_Menu.AddAuditSet(Int32.Parse(this.lstBox.SelectedItem.Value), this.ctsbAdd0.Text.Trim());

                    if (intCount == 0)
                    {
                        this.lstBox_SelectedIndexChanged(this.lstBox, null);
                        this.ctsbAdd0.Text = "";
                    }
                    else if (intCount > 0)
                    {
                        this.lblJs.Text = "<script>alert('此确认岗位已存在，不能重复！')</script>";
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('设置失败，请重试！')</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('"+ strErrorMsg +"')</script>";
                }
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择要设置的单据！')</script>";
        }
    }

    public void UpdateAuditSet(object sender, ImageClickEventArgs e)
    {
        if (this.lstBox.SelectedIndex != -1)
        {
            int intIndex = Int32.Parse(((ImageButton)sender).CommandName);
            int intItem = Int32.Parse(((ImageButton)sender).CommandArgument);

            if (intIndex == 1)
            {
                this.lstBox_SelectedIndexChanged(this.lstBox, null);
            }
            else
            {
                CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intItem].FindControl("chkRsSelect");
                ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intItem].FindControl("ibtnRsEdit");
                ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intItem].FindControl("ibtnRsCancel");
                ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intItem].FindControl("ibtnRsServer");
                CuTextSelectBox txtBox = (CuTextSelectBox)this.dgrdResult.Items[intItem].FindControl("ctsbRs0");

                if (intIndex == 2)
                {
                    chkRsSelect.Visible = ibtnRsEdit.Visible = false;
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                    txtBox.ReadOnly = false;
                    txtBox.Text = txtBox.ToolTip;
                }
                else
                {
                    //数据验证
                    ArrayList controlArray = new ArrayList();
                    controlArray.Add(txtBox);

                    string strErrorMsg = this.DataValidator(controlArray);

                    if (strErrorMsg == "")
                    {
                        int intID = Int32.Parse(this.dgrdResult.Items[intItem].Cells[1].Text.Trim());

                        ClsMenuDAL cls_Menu = new ClsMenuDAL();
                        int intCount = cls_Menu.UpdateAuditSet(intID, txtBox.Text.Trim());

                        if (intCount == 0)
                        {
                            this.lstBox_SelectedIndexChanged(this.lstBox, null);
                        }
                        else if (intCount > 0)
                        {
                            this.lblJs.Text = "<script>alert('此确认岗位已存在，不能重复！')</script>";
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('修改失败，请重试！')</script>";
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('" + strErrorMsg + "')</script>";
                    }
                }
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择要修改的单据！')</script>";
        }
    }

    private void DeleteAuditSet(int Command)
    {
        if (this.lstBox.SelectedIndex != -1)
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                if (IsChecked)
                {
                    Boolean bolIsSucceed = false;
                    int intID = Int32.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());

                    if (Command == 0)//不彻底作废
                    {
                        bolIsSucceed = cls_Menu.DeleteAuditSet(intID, 0);
                    }
                    else//彻底作废
                    {
                        bolIsSucceed = cls_Menu.DeleteAuditSet(intID, 1);
                    }

                    if (!bolIsSucceed)
                    {
                        this.lblJs.Text = "<script>alert('作废记录失败，请重试！')</script>";
                    }
                }
            }

            //重新绑定主表结果
            this.lstBox_SelectedIndexChanged(this.lstBox, null);
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择要作废的单据设置！')</script>";
        }
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        break;
                }
            }
        }
    }

    private void BindItemsDataSet(string CurrUserID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetNoSetWritByCurrUserID(CurrUserID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.lstBox.Items.Clear();
            this.lstBox.DataTextField = "m_item_name";
            this.lstBox.DataValueField = "m_item_id";
            this.lstBox.DataSource = dst;
            this.lstBox.DataBind();
        }
        else
        {
            this.lblJs.Text = "<script>alert('获取单据类型菜单项失败，请关闭后重试！');window.close();</script>";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.BindItemsDataSet(strUserID);//绑定单据数据集
            }
            else
            {
                this.lblJs.Text = "";
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        if (this.lstBox.SelectedIndex != -1)
        {
            this.panAddResult.Visible = true;
            this.lblJs.Text = "<script>document.getElementById('" + this.ctsbAdd0.ClientID + "_txtBox').focus();</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择要设置的单据！')</script>";
        }
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteAuditSet(intCommandName);
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            this.panAddResult.Visible = false;
            this.ctsbAdd0.Text = "";
            this.BindItemsDataSet(strUserID);//绑定单据数据集
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindAuditSetByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void lstBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        int intID = Int32.Parse(this.lstBox.SelectedItem.Value);

        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetAuditSetMessageDataSet(intID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindAuditSetByDataView(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
        }
    }

    private void BindAuditSetByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            string strButtonEdit = this.hidButtonEdit.Value;

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");

            if (strButtonEdit.Trim() == "1")
            {
                if (this.panDelete.Visible)
                {
                    chkRsSelect.Visible = true;
                }

                ibtnRsEdit.Visible = true;
            }
            else if (this.panDelete.Visible)
            {
                chkRsSelect.Visible = true;
            }
        }
    }
}
